package com.financing.wap.service.payment.baofoo.demo;

import com.financing.wap.service.payment.baofoo.BaofooService;
import com.financing.wap.service.payment.baofoo.model.BaofooConfig;
import com.financing.wap.service.payment.baofoo.util.JSONUtils;
import com.financing.wap.util.WeChatGiftXMLUtil;

import net.sf.json.JSONObject;

public class BaofuBindcardService {
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		
		String mid="100000178";
		String tid="100000859";
		String transno="2"+System.currentTimeMillis();
		String transid="3"+System.currentTimeMillis();
		String ip="10.0.20.80";
		String datestr="20151101102131";
		
		String idholder="张苏";
		String idcard="320721198404120094";
		String mobile="13585928439";
		String bankcode="ABC";
		String acc_no="6228480038712021476";
//		String acc_no="6217920108753414";
//		String bankcode="SPDB";
	
		
//		
//		String idholder="史云来 ";
//		String idcard="532424198107241651";
//		String acc_no="6225210111643566";
//		String bankcode="SPDB";
//		String mobile="18616559102";
		
		String prifx="/Users/zhangsu/ranyunsvn/juyoubao/develop/appserver/appserver/src/main/resources/config/";
//		String prifx="/root/config/";
		String prikeyPath=prifx+"m_pri.pfx";
		String cerPath = prifx+"baofoo_pub.cer";
		String pwd="123456";
		String postUrl = "https://tgw.baofoo.com/livesplatform/api/backTransRequest";//测试环境
		
		
		
		String nokeyParam ="version=4.0.0.0&member_id="+mid+"&terminal_id="+tid+"&txn_type=3001&txn_sub_type=01341&data_type=json";
		String keyParam ="{\"txn_sub_type\":\"01341\",\"biz_type\":\"0000\",\"terminal_id\":\""+tid+"\",\"member_id\":\""+mid+"\",\"trans_serial_no\":\""+transno+"\",\"trans_id\":\""+transid+"\",\"acc_no\":\""+acc_no+"\",\"mobile\":\""+mobile+"\",\"id_card_type\":\"01\",\"id_card\":\""+idcard+"\",\"id_holder\":\""+idholder+"\",\"card_type\":\"101\",\"valid_date\":\"\",\"valid_no\":\"\",\"pay_code\":\""+bankcode+"\",\"trade_date\":\""+datestr+"\",\"clientIp\":\""+ip+"\"}";
		
		System.out.println(keyParam);
		//"https://public.baofoo.com/livesplatform/api/backTransRequest"; //正式
		
		String getParam= ServeService.getParam(nokeyParam, keyParam, prikeyPath, pwd);
		System.out.println("请求参数："+getParam);
		String return_string = ServeService.post(postUrl, getParam);
		System.out.println("返回内容："+return_string);
		String decryptStr = ServeService.DeResponseStr(return_string, cerPath);
		System.out.println("解密返回内容："+decryptStr);
		JSONObject json = JSONObject.fromObject(decryptStr);
		System.out.println(JSONUtils.getStr(json, "resp_code"));
		System.out.println(JSONUtils.getStr(json, "resp_msg"));
//		0205
//		身份验证失败(0005:身份验证失败:银行卡信息不匹配!)
		
//		0000
//		验证成功
	}
	
public static JSONObject bindcard(String mid,String tid,String transno,String transid,String datestr,String idholder,String idcard,String mobile,String bankcode,String acc_no) throws Exception {
		
		String ip="10.0.20.80";
		String cerPath = BaofooService.class.getClassLoader().getResource(BaofooConfig.getInstance().getCerPath()).getPath();
		String prikeyPath = BaofooService.class.getClassLoader().getResource(BaofooConfig.getInstance().getKeyPath()).getPath();//商户私钥
		String pwd = BaofooConfig.getInstance().getKeyPwd();//商户私钥密码
		
//		String postUrl = "https://tgw.baofoo.com/livesplatform/api/backTransRequest";//测试环境
		String postUrl = BaofooConfig.getInstance().getBindcardUrl();//支付、查询请求地址
		
		
		
		String nokeyParam ="version=4.0.0.0&member_id="+mid+"&terminal_id="+tid+"&txn_type=0431&txn_sub_type=01&data_type=json";
		String keyParam ="{\"txn_sub_type\":\"01\",\"biz_type\":\"0000\",\"terminal_id\":\""+tid+"\",\"member_id\":\""+mid+"\",\"trans_serial_no\":\""+transno+"\",\"trans_id\":\""+transid+"\",\"acc_no\":\""+acc_no+"\",\"mobile\":\""+mobile+"\",\"id_card_type\":\"01\",\"id_card\":\""+idcard+"\",\"id_holder\":\""+idholder+"\",\"card_type\":\"101\",\"valid_date\":\"\",\"valid_no\":\"\",\"pay_code\":\""+bankcode+"\",\"trade_date\":\""+datestr+"\",\"clientIp\":\""+ip+"\"}";
		
		System.out.println(keyParam);
		//"https://public.baofoo.com/livesplatform/api/backTransRequest"; //正式
		
		String getParam= ServeService.getParam(nokeyParam, keyParam, prikeyPath, pwd);
		System.out.println("请求参数："+getParam);
		String return_string = ServeService.post(postUrl, getParam);
		System.out.println("返回内容："+return_string);
		String decryptStr = ServeService.DeResponseStr(return_string, cerPath);
		System.out.println("解密返回内容："+decryptStr);
		JSONObject json = JSONObject.fromObject(decryptStr);
		System.out.println(JSONUtils.getStr(json, "resp_code"));
		System.out.println(JSONUtils.getStr(json, "resp_msg"));
		return json;
//		0205
//		身份验证失败(0005:身份验证失败:银行卡信息不匹配!)
		
//		0000
//		验证成功
	}
}
